using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using NUnit.Framework;
using NUnit.Extensions.Forms;
using System.Drawing.Drawing2D;
using DigitalTrainSuite.UserControls;

namespace DigitalTrainSuite
{

	public enum Tools
	{
		MoveTrack,
		PlacingTrack,
		Delete,
		PlacingControl
	}

    
	[TestFixture]
	public partial class FormDesigner : Form
	{

	#region private members

		private DataSetLayout data;

	#endregion

	#region public events

		public event EventHandler buttonDesignerClick;
		public event EventHandler buttonControllerClick;
		public event EventHandler buttonLayoutClick; 

	#endregion

	#region public methods

        public FormDesigner()
        {
            this.data = new DataSetLayout();
            InitializeComponent();
            this.userControlLayout.Data = data;
            this.userControlLayout.loadLayout();
            this.labelLayoutFilename.Text = Properties.Settings.Default.LayoutFilePath;
            this.userControlLayout.EnableEdit = true;

           
            
        }

		public FormDesigner(DataSetLayout data)
		{
			this.data = data;
			InitializeComponent();
			this.userControlLayout.Data = data;
			this.userControlLayout.loadLayout();
			this.labelLayoutFilename.Text = Properties.Settings.Default.LayoutFilePath;
			this.userControlLayout.EnableEdit = true;

			foreach (DataSetLayout.TrainRow row in this.data.Train)
			{
				UserControlTrainInfo control = new UserControlTrainInfo();
				control.Row = row;
				this.flowLayoutPanelTrains.Controls.Add(control);
			}
            
            //Color Test
            //this.buttonOpen.BackColor = Properties.Settings.Default.ButtonColorBlue;
            //this.buttonSave.BackColor = Properties.Settings.Default.ButtonColorGreen;
            //Graphics g = this.flowLayoutPanelLayout.CreateGraphics();
            //Rectangle tabPageRectangle = tabControlMain.GetTabRect(0);
            //LinearGradientBrush  backColorBrush;

            //backColorBrush = new LinearGradientBrush(tabPageRectangle, Color.Blue, Color.White, LinearGradientMode.Horizontal);
            //g.FillRectangle(backColorBrush, tabPageRectangle);

            //this.labelLayoutTitle.ForeColor = Properties.Settings.Default.TextColorBlue;
            //this.BackColor = Properties.Settings.Default.FormBackColor;
            //this.buttonController.BackColor = Properties.Settings.Default.FormBackColor;
            //this.flowLayoutPanelLayout.BackColor = Properties.Settings.Default.TabsColor;
            //this.panelMain.BackColor = Properties.Settings.Default.FormBackColor;
		}

		public void saveLayout()
		{
			this.data.TurnoutControl.Clear();
			this.data.RFIDSensor.Clear();
			this.userControlLayout.saveLayout(this.data);
			this.data.WriteXml(this.labelLayoutFilename.Text);
		}

		public void saveLayout(string filename)
		{
			this.data.TurnoutControl.Clear();
			this.data.RFIDSensor.Clear();
			this.userControlLayout.saveLayout(this.data);
			this.data.WriteXml(filename);
			this.labelLayoutFilename.Text = filename;
		}

		public void openLayout(string filename)
		{
			this.data.Clear();
			this.data.ReadXml(filename);
			this.userControlLayout.loadLayout();
			this.labelLayoutFilename.Text = filename;
		}

	#endregion

	#region private event handlers

		private void checkBoxEdit_CheckedChanged(object sender, EventArgs e)
		{
			this.userControlLayout.EnableEdit = this.checkBoxEdit.Checked;
		}

		private void buttonOpen_Click(object sender, EventArgs e)
		{
			this.openFileDialog.ShowDialog();
		}

		private void buttonSave_Click(object sender, EventArgs e)
		{
			this.saveLayout();
		}

		private void checkBoxViewAddresses_CheckedChanged(object sender, EventArgs e)
		{
			this.userControlLayout.ViewAddresses = this.checkBoxViewAddresses.Checked;
		}

		private void openFileDialog_FileOk(object sender, CancelEventArgs e)
		{
			this.openLayout(this.openFileDialog.FileName);
		}

		private void FormDesigner_Load(object sender, EventArgs e)
		{

		}

		private void checkBoxViewControls_CheckedChanged(object sender, EventArgs e)
		{
			this.userControlLayout.ViewControls = this.checkBoxViewControls.Checked;
		}

		private void buttonController_Click(object sender, EventArgs e)
		{
			if (this.buttonControllerClick != null)
			{
				this.buttonControllerClick(sender, e);
			}
		}

		private void comboBoxZoom_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.userControlLayout.Zoom = Convert.ToDouble(this.comboBoxZoom.Text.Substring(0, this.comboBoxZoom.Text.Length - 1));
		}

		private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.userControlLayout.SnapResolution = Convert.ToInt32(this.comboBoxSnapRes.Text.Substring(0, this.comboBoxSnapRes.Text.Length - 2));
		} 

	#endregion

	#region NUnitForms Tests

		[Test]
		public void Test01_PlaceTrack()
		{
			FormDesigner form = new FormDesigner(this.data);
			form.Show();

			ControlTester placeTrack = new ControlTester("userControlPlaceTrackRightCurveR1", form);
			placeTrack.FireEvent("Click", new EventArgs());
			Assert.IsNotNull(form.userControlLayout.PlacingTrack, "The Canvas was not notified that a track tool was selected.\n Placing Track was NULL.");
			Assert.AreEqual(360, form.userControlLayout.PlacingTrack.radius, "Radius for placing track not set properly");
			Assert.AreEqual(0, form.userControlLayout.PlacingTrack.rotation, "Rotation no initialized to zero");

			ControlTester canvas = new ControlTester("userControlLayout", form);
			canvas.FireEvent("MouseMove", new MouseEventArgs(MouseButtons.None, 0, 200, 200, 0));
			Assert.AreEqual(1000, form.userControlLayout.PlacingTrack.locationX, "Scaled placement incorrect");
			Assert.AreEqual(1000, form.userControlLayout.PlacingTrack.locationY, "Scaled placement incorrect");

			int numTrackRows = form.data.Track.Count;
			canvas.FireEvent("Click", new MouseEventArgs(MouseButtons.Left, 1, 200, 200, 0));
			Assert.IsNull(form.userControlLayout.PlacingTrack, "PlacingTrack was not reset to null");
			Assert.AreEqual(numTrackRows + 1, form.data.Track.Count, "Track Table not modified correctly");
		}

		[Test]
		public void Test02_ZoomChange()
		{
			FormDesigner form = new FormDesigner(this.data);
			form.Show();

			ComboBoxTester zoomControl = new ComboBoxTester("comboBoxZoom", form);
			zoomControl.Select(1);
			Assert.AreEqual(20, form.userControlLayout.Zoom, "Zoom was not updated");
			Assert.AreEqual(5, form.userControlLayout.DrawingScale, "Drawing Scale was not updated");
		}

		[Test]
		public void Test03_SnapGridChange()
		{
			FormDesigner form = new FormDesigner(this.data);
			form.Show();

			ComboBoxTester snapControl = new ComboBoxTester("comboBoxSnapRes", form);
			snapControl.Select(13);
			Assert.AreEqual(70, form.userControlLayout.SnapResolution, "Snap was not updated");
			Assert.AreEqual(70, form.userControlLayout.gridsSnapRes(), "Grid not reset");
		}

		#endregion

		private void userControlLayout_Load(object sender, EventArgs e)
		{

		}

		private void tabControlMain_SelectedIndexChanged(object sender, EventArgs e)
		{

		}

		private void buttonMoveTrack_Click(object sender, EventArgs e)
		{
			this.userControlLayout.SelectedTool = Tools.MoveTrack;
			this.userControlLayout.PlacingTrack = null;
		}

        private void groupBoxOther_MouseHover(object sender, EventArgs e)
        {

        }

        #region Form Coloring methods

        private void flowLayoutPanelLayout_Paint(object sender, PaintEventArgs e)
        {
            //Graphics b = this.buttonOpen.CreateGraphics();
            Graphics g = e.Graphics;
            Graphics test = this.userControlLayout.CreateGraphics();
            Font Arial = new Font("Arial", 12);
            //Rectangle tabPageRectangle = tabControlMain.GetTabRect(1);
            //Rectangle tabPageRectangle = new Rectangle(this.flowLayoutPanelLayout.Location.X, this.flowLayoutPanelLayout.Location.Y, this.flowLayoutPanelLayout.Size.Width, this.flowLayoutPanelLayout.Size.Height);
            Rectangle tabPageRectangle = new Rectangle(0, 0, this.flowLayoutPanelLayout.Size.Width, this.flowLayoutPanelLayout.Size.Height);
            Rectangle buttonOpenRectangle = new Rectangle(0, 0, this.buttonOpen.Size.Width, this.buttonOpen.Size.Height);
            Rectangle testRect = new Rectangle(100, 10, 61, 23);
            LinearGradientBrush backColorBrush;
            LinearGradientBrush buttonColorBrushGray;
            LinearGradientBrush buttonColorBrushBlue;
            SolidBrush brush1 = new SolidBrush(Color.Red);
            SolidBrush brush2 = new SolidBrush(Color.Black);

            backColorBrush = new LinearGradientBrush(tabPageRectangle, Properties.Settings.Default.TabsPageColor, Color.White, LinearGradientMode.ForwardDiagonal);
            buttonColorBrushGray = new LinearGradientBrush(buttonOpenRectangle, Color.Silver, Color.LightGray, LinearGradientMode.Vertical);
            buttonColorBrushBlue = new LinearGradientBrush(buttonOpenRectangle, Color.CadetBlue, Color.AliceBlue, LinearGradientMode.Vertical);
            g.FillRectangle(backColorBrush, tabPageRectangle);
            //test.FillRectangle(buttonColorBrush, testRect);

            //BUTTON TEST
            //Trying to get the text to show about the painted Rectangle
           // b.FillRectangle(brush1, buttonOpenRectangle);
            //b.FillRectangle(buttonColorBrushBlue, 1, 1, 59, 21);
            //b.DrawString("Stop", Arial, brush2, 5, 5);
            //BringToFront(this.buttonOpen.Text);
            //this.buttonOpen.TextImageRelation = TextImageRelation.TextAboveImage;
            //this.buttonOpen.Text = BringToFront();
           // this.buttonOpen.TextImageRelation = BringToFront();

            
            //this.buttonOpen.FlatAppearance.BorderColor = Color.DarkGray;
            //this.buttonOpen.FlatAppearance.MouseOverBackColor = Color.YellowGreen;
            //this.BackColor = Properties.Settings.Default.FormBackColor;
            //this.panelMain.BackColor = Properties.Settings.Default.FormBackColor;
        }

        private void panelMain_Paint(object sender, PaintEventArgs e)

        {
            Graphics g = e.Graphics;
            LinearGradientBrush backColorBrush;
            Rectangle panelMainRectangle = new Rectangle(0, 0, panelMain.Size.Width, panelMain.Size.Height);
            
            backColorBrush = new LinearGradientBrush(panelMainRectangle, Properties.Settings.Default.FormBackColor, Color.LightGray, LinearGradientMode.Vertical);
            g.FillRectangle(backColorBrush,panelMainRectangle);

        }

        private void buttonOpen_Paint(object sender, PaintEventArgs e)
        {
            //Graphics b = this.buttonOpen.CreateGraphics();
            //Graphics b = this.CreateGraphics();
            Graphics b = e.Graphics;
            Rectangle buttonRectangle;
            LinearGradientBrush buttonColorBrushGray;
            LinearGradientBrush buttonColorBrushBlue;
            SolidBrush brush1 = new SolidBrush(Color.Red);
            SolidBrush brush2 = new SolidBrush(Color.Black);

            buttonRectangle = new Rectangle(0,0, this.buttonOpen.Size.Width, this.buttonOpen.Size.Height);
            buttonColorBrushGray = new LinearGradientBrush(buttonRectangle, Color.Silver, Color.LightGray, LinearGradientMode.Vertical);
            buttonColorBrushBlue = new LinearGradientBrush(buttonRectangle, Color.CadetBlue, Color.AliceBlue, LinearGradientMode.Vertical);
            //b.FillRectangle(buttonColorBrushBlue, 1, 1, 59, 21);
            
        }

        private void tabPagePlace_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Rectangle tabPageRectangle = new Rectangle(0, 0, this.flowLayoutPanelLayout.Size.Width, this.flowLayoutPanelLayout.Size.Height);
            LinearGradientBrush backColorBrush;

            backColorBrush = new LinearGradientBrush(tabPageRectangle, Properties.Settings.Default.TabsPageColor, Color.White, LinearGradientMode.ForwardDiagonal);
            g.FillRectangle(backColorBrush, tabPageRectangle);


        }

		
		private void tabPageTrains_Paint(object sender, PaintEventArgs e)
		{
			Graphics g = e.Graphics;
			Rectangle tabPageRectangle = new Rectangle(0, 0, this.flowLayoutPanelLayout.Size.Width, this.flowLayoutPanelLayout.Size.Height);
			LinearGradientBrush backColorBrush;

			backColorBrush = new LinearGradientBrush(tabPageRectangle, Properties.Settings.Default.TabsPageColor, Color.White, LinearGradientMode.ForwardDiagonal);
			g.FillRectangle(backColorBrush, tabPageRectangle);
		}

        #endregion

        private void buttonDeleteTrack_Click(object sender, EventArgs e)
		{
			this.userControlLayout.SelectedTool = Tools.Delete;
			this.userControlLayout.PlacingTrack = null;      

		}

		private void buttonAddTrain_Click(object sender, EventArgs e)
		{
			DataSetLayout.TrainRow row = this.data.Train.NewTrainRow();
			row.name = "Name";
			row.locoAddress = 1;
			UserControlTrainInfo control = new UserControlTrainInfo();
			control.Row = row;
			this.data.Train.AddTrainRow(row);
			this.flowLayoutPanelTrains.Controls.Add(control);
		}

		private void buttonSaveAs_Click(object sender, EventArgs e)
		{
			this.saveFileDialog.ShowDialog();
		}

		private void saveFileDialog_FileOk(object sender, CancelEventArgs e)
		{
			this.saveLayout(this.saveFileDialog.FileName);
		}

		private void buttonNew_Click(object sender, EventArgs e)
		{
			this.data.Clear();
			this.userControlLayout.Controls.Clear();
			this.userControlLayout.Invalidate();
			this.flowLayoutPanelTrains.Controls.Clear();
			this.labelLayoutFilename.Text = "newLayout.xml";
		}

		private void checkBoxSnapToPart_CheckedChanged(object sender, EventArgs e)
		{
			this.userControlLayout.SnapToParts = this.checkBoxSnapToPart.Checked;
		}

		private void FormDesigner_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				this.userControlLayout.SelectedTool = Tools.MoveTrack;
			}
		}

		private void buttonAddTurnoutControl_Click(object sender, EventArgs e)
		{
			this.userControlLayout.SelectedTool = Tools.PlacingControl;
			UserControlTurnoutController control = new UserControlTurnoutController();
			control.Mode = CanvasMode.Designer;
			this.userControlLayout.CurrentControl = control;
			
		}

		private void buttonAddSensorControl_Click(object sender, EventArgs e)
		{
			this.userControlLayout.SelectedTool = Tools.PlacingControl;
			this.userControlLayout.CurrentControl = new UserControlSensorController();
		}

		

		


       

       

        

	}
}